import piniaPersistConfig from "@/config/piniaPersist";
import { createPinia, defineStore } from "pinia";
import piniaPluginPersistedstate from "pinia-plugin-persistedstate";
import { PublicState } from "../interface";
// defineStore 调用后返回一个函数，调用该函数获得 Store 实体
export const PublicStore = defineStore({
	// id: 必须的，在所有 Store 中唯一
	id: "sansan_public_global",
	// state: 返回对象的函数
	state: (): PublicState => ({
		expire: "",
		token: "",
		// userInfo
		userInfo: {
			userId: "",
			username: "",
			ssex: "",
			alias: "",
			avatar: "",
			status: "",
			lastLoginTime: "",
			deptName: "",
			deptIds: "",
			validDate: ""
		}
	}),
	getters: {},
	actions: {
		setExpire(expire: string) {
			this.expire = expire;
		},
		// setToken
		setToken(token: string) {
			this.token = token;
		},
		// setUserInfo
		setUserInfo(userInfo: User.UserInfo) {
			this.userInfo = userInfo;
		}
	},
	persist: piniaPersistConfig("sansan_public_global")
});

// piniaPersist(持久化)
const pinia = createPinia();
pinia.use(piniaPluginPersistedstate);

export default pinia;
